package com.bsj.power.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.PortalLoginLogSearchDTO;
import com.bsj.power.common.def.entity.portalLoginLog.PortalLoginLog;
import com.bsj.power.common.def.vo.sys.PortalLoginLogVO;
import com.bsj.power.common.def.vo.sys.excel.PortalLoginLogExcelVO;
import com.bsj.power.common.mapper.PortalLoginLogMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.system.service.PortalLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description PortalLoginLogServiceImpl
 * @time 2024/10/15 15:21
 */
@Service
public class PortalLoginLogServiceImpl implements PortalLoginLogService {

    @Resource
    private PortalLoginLogMapper portalLoginLogMapper;


    /**
     * 门户单点登录日志
     *
     * @param portalLoginLogSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.sys.PortalLoginLogVO>>
     * @author ljx
     * @time 2024/10/15 15:22
     */
    @Override
    public JsonResult<List<PortalLoginLogVO>> page(PortalLoginLogSearchDTO portalLoginLogSearchDTO) {
        IPage<PortalLoginLog> page = new Page<>(portalLoginLogSearchDTO.getPageNumber(), portalLoginLogSearchDTO.getPageSize());
        QueryWrapper<PortalLoginLog> queryWrapper = new QueryWrapper<>();
        String ip = portalLoginLogSearchDTO.getIp();
        String loginId = portalLoginLogSearchDTO.getLoginId();
        String startTime = portalLoginLogSearchDTO.getStartTime();
        String endTime = portalLoginLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(loginId)) {
            queryWrapper.lambda().like(PortalLoginLog::getLoginId, loginId);
        }
        if (StringUtils.isNotBlank(ip)) {
            queryWrapper.lambda().like(PortalLoginLog::getIp, ip);
        }
        if (!StringUtils.isAllBlank(startTime, endTime)) {
            queryWrapper.lambda().between(PortalLoginLog::getLoginTime, startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(PortalLoginLog::getLoginTime);
        List<PortalLoginLogVO> portalLoginLogVOS = new ArrayList<>();
        IPage<PortalLoginLog> iPage = portalLoginLogMapper.selectPage(page, queryWrapper);
        List<PortalLoginLog> portalLoginLogs = iPage.getRecords();
        if (!CollectionUtils.isEmpty(portalLoginLogs)) {
            portalLoginLogVOS = portalLoginLogs.stream().map(portalLoginLog -> {
                PortalLoginLogVO portalLoginLogVO = new PortalLoginLogVO();
                BeanUtils.copyProperties(portalLoginLog, portalLoginLogVO);
                return portalLoginLogVO;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(portalLoginLogVOS, iPage.getTotal());
    }

    /**
     * 获取单点登录记录导出数据
     *
     * @param portalLoginLogSearchDTO
     * @return java.util.List<com.bsj.power.common.def.vo.sys.excel.PortalLoginLogExcelVO>
     * @author ljx
     * @time 2024/10/15 15:51
     */
    @Override
    public List<PortalLoginLogExcelVO> getExportData(PortalLoginLogSearchDTO portalLoginLogSearchDTO) {
        QueryWrapper<PortalLoginLog> queryWrapper = new QueryWrapper<>();
        String ip = portalLoginLogSearchDTO.getIp();
        String loginId = portalLoginLogSearchDTO.getLoginId();
        String startTime = portalLoginLogSearchDTO.getStartTime();
        String endTime = portalLoginLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(loginId)) {
            queryWrapper.lambda().like(PortalLoginLog::getLoginId, loginId);
        }
        if (StringUtils.isNotBlank(ip)) {
            queryWrapper.lambda().like(PortalLoginLog::getIp, ip);
        }
        if (!StringUtils.isAllBlank(startTime, endTime)) {
            queryWrapper.lambda().between(PortalLoginLog::getLoginTime, startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(PortalLoginLog::getLoginTime);
        List<PortalLoginLogExcelVO> portalLoginLogExcelVOS = new ArrayList<>();
        List<PortalLoginLog> portalLoginLogs = portalLoginLogMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(portalLoginLogs)) {
            portalLoginLogExcelVOS = portalLoginLogs.stream().map(portalLoginLog -> {
                Integer isSuccess = portalLoginLog.getIsSuccess();
                PortalLoginLogExcelVO portalLoginLogExcelVO = new PortalLoginLogExcelVO();
                BeanUtils.copyProperties(portalLoginLog, portalLoginLogExcelVO);
                portalLoginLogExcelVO.setIsSuccess(isSuccess.equals(NumericalConstants.ONE) ? "成功" : "失败");
                portalLoginLogExcelVO.setLoginTime(DateUtils.dateToStr(portalLoginLog.getLoginTime()));
                return portalLoginLogExcelVO;
            }).collect(Collectors.toList());
        }
        return portalLoginLogExcelVOS;
    }
}
